/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package record;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author mgeden
 */
public class OptionPrefer {
 private byte fontSize,volumeLevel,screenTop,hijriCorrection;
 private boolean isFullScreen,isMilitary, isItFirstTime=false;
 public OptionPrefer () 
    {
        //loadFromDataBase();
    } 
 public void setOptionParameters(byte fontSize,byte volumeLevel,byte screenTop,byte hijriCorrection, boolean isFullScreen,boolean isMilitary)
    { 
        this.fontSize=fontSize;
        this.volumeLevel=volumeLevel;
        this.screenTop=screenTop;
        this.hijriCorrection=hijriCorrection; 
        this.isFullScreen=isFullScreen;
        this.isMilitary=isMilitary;
        this.isItFirstTime=false;
        
        //System.out.println(isMilitary);
    }
 

 public byte getFontSize() {
        return fontSize;
    }
 public byte getVolumeLevel() {
        return volumeLevel;
   }
 public byte getScreenTop() {
       return screenTop;
   }
  public byte getHijriCorrection() {
       return hijriCorrection;
   }
 public boolean getIsFullScreen() {
        return isFullScreen;
   }
  public boolean getIsMilitary() {
        return isMilitary;
   }
 public boolean getIsItFirstTime() {
        return isItFirstTime;
    }

 public void loadOptionDataBase() {
        RecordStore recordStore = null;
        DataInputStream inputStream = null;
        try {
	    recordStore = RecordStore.openRecordStore("optionPrefer", false);
            byte[] bytes = recordStore.getRecord(1);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            inputStream = new DataInputStream(bais);
           // isDSTOn=inputStream.readBoolean();
            fontSize=inputStream.readByte();
            volumeLevel=inputStream.readByte();
            screenTop=inputStream.readByte();
            hijriCorrection=inputStream.readByte();
          
            isFullScreen=inputStream.readBoolean();
            isMilitary=inputStream.readBoolean();
        } catch (Exception ex) {
          fontSize=0;//0 for 0 medium 16 large
          volumeLevel=50;  
          screenTop=0;
          hijriCorrection=0;
          isFullScreen=false;
          isMilitary=true;
          isItFirstTime=true;
          //System.out.println("calıstı");
        } finally {
            try {inputStream.close();} catch (Exception ex) {}
            try {recordStore.closeRecordStore();} catch (Exception ex) {}
        }
    }
 public void saveToDataBase ()
    {
        RecordStore recordStore;
        DataOutputStream outputStream;
        try {
            recordStore = RecordStore.openRecordStore("optionPrefer", true);
        } catch (RecordStoreException rsc) {
            return;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        outputStream = new DataOutputStream(baos);
        try {
       //     outputStream.writeBoolean(isDSTOn);
            outputStream.writeByte(fontSize);
            outputStream.writeByte(volumeLevel);
            outputStream.writeByte(screenTop);
            outputStream.writeByte(hijriCorrection);
            outputStream.writeBoolean(isFullScreen);
            outputStream.writeBoolean(isMilitary);
            byte[] bytes = baos.toByteArray();
            
            if (recordStore.getNumRecords() == 0)
                recordStore.addRecord(bytes, 0, bytes.length);
            else
                recordStore.setRecord(1, bytes, 0, bytes.length);
        } catch (Exception exception) {
        } finally {
            try {outputStream.close();} catch (Exception ex) {}
            try {recordStore.closeRecordStore();} catch (Exception ex) {}
        }
    }
}
